﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.DataSourcesRaster;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.Geodatabase;
using System.IO;

namespace 实习九
{
    public partial class MyMap : Form
    {
        public MyMap()
        {
            ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.EngineOrDesktop);
            InitializeComponent();
        }

        private void AxLicenseControl2_Enter(object sender, EventArgs e)
        {

        }

        private void MyMap_Load(object sender, EventArgs e)
        {

        }

        private void addshpdataToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();//好像是创建了一个文件对话框
            openFileDialog.Filter = "shapefile文件(*shp)|*.shp";//添加筛选器，筛选后缀
            DialogResult dialogResult = openFileDialog.ShowDialog();
            if (dialogResult != DialogResult.OK) { return; }  //点击了关闭键而不是确认键
            string path = openFileDialog.FileName;  //文件对话框返回的文件路径
            string folder = System.IO.Path.GetDirectoryName(path);//这句话提取了路径？
            string filename = System.IO.Path.GetFileName(path); //提取文件名

            IWorkspaceFactory workspacefactory = new ShapefileWorkspaceFactory();//实例化了一个工厂类
            IWorkspace workspace = workspacefactory.OpenFromFile(folder, 0);//这句话是啥意思
            if(workspace==null)
            {
                MessageBox.Show("无法打开工作空间");
                return;
            }
            IFeatureWorkspace featureWorkspace = workspace as IFeatureWorkspace;//啥意思
            IFeatureClass featureClass = featureWorkspace.OpenFeatureClass(filename);
            if (featureClass==null)
            {
                MessageBox.Show("无法打开矢量数据集");
                return;
            }
            IFeatureLayer featureLayer = new FeatureLayerClass();

            featureLayer.FeatureClass = featureClass;
            featureLayer.Name = featureClass.AliasName;
            ILayer layer = featureLayer as ILayer;
            axMapControl1.Map.AddLayer(layer);
            axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);



        }

        private void SimpleValueRenderMenuItem_Click(object sender, EventArgs e)
        {
            esriTOCControlItem tocChoosedItem = esriTOCControlItem.esriTOCControlItemLayer;
            IBasicMap basicMap = null;
            ILayer tocChoosedLayer = null;
            object other = new object();
            object index = new object();
            axTOCControl1.GetSelectedItem(
                ref tocChoosedItem,
                ref basicMap,
                ref tocChoosedLayer,
                ref other,
                ref index
            );

            if (tocChoosedLayer is IFeatureLayer)
            {
                FieldSelectDialog fieldSelectDialog = new FieldSelectDialog(tocChoosedLayer as IFeatureLayer);
                DialogResult dialogResult = fieldSelectDialog.ShowDialog();
                if(dialogResult == DialogResult.OK)
                {
                    defineUniqueValueRender(tocChoosedLayer as IFeatureLayer, fieldSelectDialog.selectFieldName());

                }
            }
            else
            {
                MessageBox.Show("未选择图层，或图层非矢量");
                return;
            }
        }
        private void simple_renderingToolStripMenuItem_Click(object sender, EventArgs e)
        {
            esriTOCControlItem tocChoosedItem = esriTOCControlItem.esriTOCControlItemLayer;//非常诡异的实例化方式
            IBasicMap basicMap = null;
            ILayer tocChoosedLayer = null;
            object other = new object();
            object index = new object();
            axTOCControl1.GetSelectedItem(
                ref tocChoosedItem,
                ref basicMap,
                ref tocChoosedLayer,
                ref other,
                ref index);

            if(tocChoosedLayer is IFeatureLayer)
            {
                defineSimpleRender(tocChoosedLayer as IFeatureLayer);
            }
            else
            {
                MessageBox.Show("未选择图层，或非矢量图层");
                return;
            }

        }
    private void defineSimpleRender(IFeatureLayer featureLayer)
        {
            IRgbColor color = new RgbColor();
            color.Red = 255;
            color.Blue = 0;
            color.Green = 0;

            ISimpleFillSymbol simpleFillSymbol = new SimpleFillSymbol();
            simpleFillSymbol.Style = esriSimpleFillStyle.esriSFSSolid;
            simpleFillSymbol.Outline.Width = 0.4;
            simpleFillSymbol.Color = color as IColor;

            ISimpleRenderer simpleRenderer = new SimpleRendererClass();
            simpleRenderer.Symbol = simpleFillSymbol as ISymbol;

            IGeoFeatureLayer geoFeatureLayer = featureLayer as IGeoFeatureLayer;
            geoFeatureLayer.Renderer = simpleRenderer as IFeatureRenderer;

            axMapControl1.Refresh();

        }

 
    }
}
